package coroutines.coroutinescope.exception

import kotlinx.coroutines.*
import java.lang.Exception

class SupervisorScopeTest {
}

fun main() {
    GlobalScope.launch(
//        CoroutineExceptionHandler { coroutineContext, throwable ->
//            println("handleException($coroutineContext, $throwable)")
//        }
    ) {
        val handler = CoroutineExceptionHandler { coroutineContext, throwable ->
            println("handleException($coroutineContext, $throwable)")
        }

        launch {
            delay(100)
            println("launch() 0")
        }

        /**
         * 主从作用域，直击抛出异常，仍然会向上传递，里面在子协程抛出则不会向外传递
         * */
        supervisorScope {
            delay(200)
            launch {
                throw Exception("哔哩哔哩")
            }
        }

        launch {
            delay(300)
            println("launch() 1")
        }
    }

    try {
        Thread.sleep(1000)
        println("main() 500ms")
    } catch (t: Throwable) {
        t.printStackTrace()
    }
}